package com.ssy.lingxi.settle.accounts.serviceimpl;

import com.ssy.lingxi.common.constant.basic.FundModeEnum;
import com.ssy.lingxi.common.constant.order.OrderPayChannelEnum;
import com.ssy.lingxi.common.exception.BusinessException;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.dto.request.CorporateAccountConfigRequest;
import com.ssy.lingxi.dto.response.CorporateAccountConfigResponse;
import com.ssy.lingxi.member.merchant.api.feign.inner.MemberInnerControllerFeign;
import com.ssy.lingxi.member.merchant.api.model.vo.inner.request.MemberFeignIdVO;
import com.ssy.lingxi.member.merchant.api.model.vo.inner.response.MemberFeignRegisterQueryVO;
import com.ssy.lingxi.order.api.feign.OrderFeignService;
import com.ssy.lingxi.order.api.model.vo.request.OrderMemberFeignVO;
import com.ssy.lingxi.order.api.model.vo.response.PlatformPayTypeFeignDetailVO;
import com.ssy.lingxi.settle.accounts.entity.CorporateAccountConfig;
import com.ssy.lingxi.settle.accounts.entity.PlatformAccountConfigDO;
import com.ssy.lingxi.settle.accounts.model.constant.AccountConfigUpdateEnum;
import com.ssy.lingxi.settle.accounts.model.vo.request.CorporateAccountConfigUpdateVO;
import com.ssy.lingxi.settle.accounts.model.vo.request.GetMemberAccountConfigVO;
import com.ssy.lingxi.settle.accounts.model.vo.response.MemberAccountConfigVO;
import com.ssy.lingxi.settle.accounts.repository.CorporateAccountConfigRepository;
import com.ssy.lingxi.settle.accounts.repository.PlatformAccountConfigRepository;
import com.ssy.lingxi.settle.accounts.service.CorporateAccountConfigService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CorporateAccountConfigServiceImpl implements CorporateAccountConfigService {
    @Resource
    CorporateAccountConfigRepository corporateAccountConfigRepository;

    @Resource
    PlatformAccountConfigRepository platformAccountConfigRepository;

    @Resource
    private MemberInnerControllerFeign memberInnerControllerFeign;

    @Resource
    private OrderFeignService orderFeignService;

    /**
     * 查询对公账户信息
     * @author wrc
     * @date 2020/9/10
     */
    @Override
    public Wrapper<CorporateAccountConfigResponse> corporateAccountConfig(Long memberId, Long memberRoleId) {
        //fixed by wn : 查询平台后台 - 会员支付策略配置
        OrderMemberFeignVO feignVO = new OrderMemberFeignVO();
        feignVO.setMemberId(memberId);
        feignVO.setRoleId(memberRoleId);
        feignVO.setPayType(null);
        feignVO.setPayChannel(OrderPayChannelEnum.OFFLINE);
        Wrapper<List<PlatformPayTypeFeignDetailVO>> paymentResult = orderFeignService.findPlatformPayments(feignVO);
        if(paymentResult.getCode() != ResponseCode.SUCCESS.getCode()) {
            return Wrapper.fail(paymentResult.getCode(), paymentResult.getMessage());
        }

        FundModeEnum fundMode = paymentResult.getData().stream().map(PlatformPayTypeFeignDetailVO::getFundMode).findFirst().orElse(FundModeEnum.PLATFORM_EXCHANGE);

        CorporateAccountConfigResponse corporateAccountConfigResponse = new CorporateAccountConfigResponse();
        if(fundMode.getCode().equals(FundModeEnum.PLATFORM_EXCHANGE.getCode())) {
            PlatformAccountConfigDO platformAccountConfigDO = platformAccountConfigRepository.findAll().stream().filter(v -> null != v && null != v.getBankAccount()).findFirst().orElse(null);
            if (null == platformAccountConfigDO) {
                return Wrapper.fail(ResponseCode.PLEASE_COMPLETE_CORPORATE_ACCOUNT_INFORMATION_CONFIGURATION_FIRST);
            }
            BeanUtils.copyProperties(platformAccountConfigDO, corporateAccountConfigResponse);
        } else {
            CorporateAccountConfig corporateAccountConfig = corporateAccountConfigRepository.findByMemberIdAndMemberRoleId(memberId, memberRoleId);
            if (null == corporateAccountConfig) {
                return Wrapper.fail(ResponseCode.PLEASE_COMPLETE_CORPORATE_ACCOUNT_INFORMATION_CONFIGURATION_FIRST);
            }
            BeanUtils.copyProperties(corporateAccountConfig, corporateAccountConfigResponse);
            // 取最新的会员名称
            MemberFeignIdVO idVO = new MemberFeignIdVO();
            idVO.setMemberId(memberId);
            Wrapper<MemberFeignRegisterQueryVO> wrapper = memberInnerControllerFeign.getMemberRegisterInfo(idVO);
            if (null != wrapper && ResponseCode.SUCCESS.getCode() == wrapper.getCode() && null != wrapper.getData()) {
                corporateAccountConfigResponse.setName(StringUtils.hasLength(wrapper.getData().getLegalPersonName()) ? wrapper.getData().getLegalPersonName() : wrapper.getData().getName());
            }
        }

        return Wrapper.success(corporateAccountConfigResponse);
    }

    /**
     * 查询当前会员对公账户信息
     * @author 伍将
     * @date 2020/10/22
     * @param sysUser:
     * @return com.ssy.lingxi.common.response.Wrapper<com.ssy.lingxi.dto.response.CorporateAccountConfigResponse>
     **/
    @Override
    public Wrapper<CorporateAccountConfigResponse> getDetail(UserLoginCacheDTO sysUser) {

        CorporateAccountConfigResponse corporateAccountConfigResponse = new CorporateAccountConfigResponse();
        CorporateAccountConfig config = corporateAccountConfigRepository.findByMemberIdAndMemberRoleId(sysUser.getMemberId(), sysUser.getMemberRoleId());
        if (null == config) {
            config = new CorporateAccountConfig();
            config.setMemberId(sysUser.getMemberId());
            config.setMemberRoleId(sysUser.getMemberRoleId());
            config.setName(sysUser.getUserName());
        }
        // 取最新的会员名称
        MemberFeignIdVO idVO = new MemberFeignIdVO();
        idVO.setMemberId(sysUser.getMemberId());
        Wrapper<MemberFeignRegisterQueryVO> wrapper = memberInnerControllerFeign.getMemberRegisterInfo(idVO);
        if (null != wrapper && ResponseCode.SUCCESS.getCode() == wrapper.getCode() && null != wrapper.getData()) {
            config.setName(StringUtils.hasLength(wrapper.getData().getLegalPersonName()) ? wrapper.getData().getLegalPersonName() : wrapper.getData().getName());
        }
        BeanUtils.copyProperties(config, corporateAccountConfigResponse);

        return Wrapper.success(corporateAccountConfigResponse);
    }

    /**
     * 结算能力 结算规则配置 对公账户修改
     *
     * @param request 请求
     * @param sysUser 登录用户
     * @author wrc
     * @date 2020/9/10
     */
    @Override
    public Wrapper<Void> corporateAccountUpdate(CorporateAccountConfigRequest request, UserLoginCacheDTO sysUser) {


        CorporateAccountConfig corporateAccountConfig = corporateAccountConfigRepository.findByMemberIdAndMemberRoleId(sysUser.getMemberId(), sysUser.getMemberRoleId());
        if (null == corporateAccountConfig) {
            corporateAccountConfig = new CorporateAccountConfig();
            corporateAccountConfig.setMemberId(sysUser.getMemberId());
            corporateAccountConfig.setMemberRoleId(sysUser.getMemberRoleId());
        }
        corporateAccountConfig.setName(request.getName());
        corporateAccountConfig.setBankAccount(request.getBankAccount());
        corporateAccountConfig.setBankDeposit(request.getBankDeposit());

        corporateAccountConfigRepository.saveAndFlush(corporateAccountConfig);

        return Wrapper.success();
    }

    /**
     * 结算能力 结算规则配置 对公账户详情修改
     * @author 伍将
     * @date 2020/12/17
     * @param sysUser:
     * @param updateVO:
     **/
    @Override
    public Wrapper<Void> corporateAccountUpdateDetail(UserLoginCacheDTO sysUser, CorporateAccountConfigUpdateVO updateVO) {

        CorporateAccountConfig configDO = corporateAccountConfigRepository.findByMemberIdAndMemberRoleId(sysUser.getMemberId(), sysUser.getMemberRoleId());
        if (null == configDO) {
            configDO = new CorporateAccountConfig();
            configDO.setMemberId(sysUser.getMemberId());
            configDO.setMemberRoleId(sysUser.getMemberRoleId());
        }

        AccountConfigUpdateEnum itemEnum = AccountConfigUpdateEnum.getItem(updateVO.getItemType());
        if (itemEnum == null) {
            throw new BusinessException(ResponseCode.SA_ACCOUNT_CONFIG_PARAM_ERROR);
        }
        switch (itemEnum) {
            case NAME:
                configDO.setName(updateVO.getItemValue());
                break;
            case BANK_ACCOUNT:
                configDO.setBankAccount(updateVO.getItemValue());
                break;
            case BANK_DEPOSIT:
                configDO.setBankDeposit(updateVO.getItemValue());
                break;
        }

        corporateAccountConfigRepository.saveAndFlush(configDO);

        return Wrapper.success();
    }

    /**
     * 获取会员对公账户配置
     * @author 伍将
     * @date 2020/12/28
     * @param getMemberAccountConfigVO:
     **/
    @Override
    public Wrapper<MemberAccountConfigVO> getMemberAccountConfig(GetMemberAccountConfigVO getMemberAccountConfigVO) {

        CorporateAccountConfig configDo = corporateAccountConfigRepository.findByMemberIdAndMemberRoleId(getMemberAccountConfigVO.getMemberId(), getMemberAccountConfigVO.getRoleId());
        if (null == configDo) {
            return Wrapper.fail(ResponseCode.PLEASE_COMPLETE_CORPORATE_ACCOUNT_INFORMATION_CONFIGURATION_FIRST);
        }

        MemberAccountConfigVO result = new MemberAccountConfigVO();
        result.setId(configDo.getId());
        result.setBankAccount(configDo.getBankAccount());
        result.setBankDeposit(configDo.getBankDeposit());
        result.setName(configDo.getName());

        // 取最新的会员名称
        MemberFeignIdVO idVO = new MemberFeignIdVO();
        idVO.setMemberId(getMemberAccountConfigVO.getMemberId());
        Wrapper<MemberFeignRegisterQueryVO> wrapper = memberInnerControllerFeign.getMemberRegisterInfo(idVO);
        if (null != wrapper && ResponseCode.SUCCESS.getCode() == wrapper.getCode() && null != wrapper.getData()) {
            result.setName(StringUtils.hasLength(wrapper.getData().getLegalPersonName()) ? wrapper.getData().getLegalPersonName() : wrapper.getData().getName());
        }

        return Wrapper.success(result);
    }

    /**
     * 获取会员对公账户配置
     * @author 伍将
     * @date 2021/2/23
     * @param getMemberAccountConfigVO:请求参数
     * @return 操作结果
     **/
    @Override
    public Wrapper<MemberAccountConfigVO> getMemberAccountByMobile(GetMemberAccountConfigVO getMemberAccountConfigVO) {
        MemberAccountConfigVO result = null;

        CorporateAccountConfig configDo = corporateAccountConfigRepository.findByMemberIdAndMemberRoleId(getMemberAccountConfigVO.getMemberId(), getMemberAccountConfigVO.getRoleId());
        if (null != configDo) {
            result = new MemberAccountConfigVO();
            result.setId(configDo.getId());
            result.setBankAccount(configDo.getBankAccount());
            result.setBankDeposit(configDo.getBankDeposit());
            result.setName(configDo.getName());
            // 取最新的会员名称
            MemberFeignIdVO idVO = new MemberFeignIdVO();
            idVO.setMemberId(getMemberAccountConfigVO.getMemberId());
            Wrapper<MemberFeignRegisterQueryVO> wrapper = memberInnerControllerFeign.getMemberRegisterInfo(idVO);
            if (null != wrapper && ResponseCode.SUCCESS.getCode() == wrapper.getCode() && null != wrapper.getData()) {
                result.setName(StringUtils.hasLength(wrapper.getData().getLegalPersonName()) ? wrapper.getData().getLegalPersonName() : wrapper.getData().getName());
            }
        }

        return Wrapper.success(result);
    }
}
